package com.beone.smart.userCenter.utils;


import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.util.Random;

/**
 * @Date: 2019/8/27
 * @Author: yangjie
 * desc:  密码安全工具类
 **/
public class EncodePwdUtils {
    //盐，用于混交md5
    private static final String slat = "&%7A2Asc*&%$$#@";

    //a37fba0e7896e73e6b851c619a4ae599
    //0c844bf8c1a3e004c83a69fd965d00e0
    public static void main(String[] args) {
        System.out.println(getMD5("11651666666"));
    }

    /**
     * spring框架里面的加密方法
     *
     * @param value 需要加密的字符串
     * @return 加密后的字符串
     */
    public static String getMD5(String value) {
        Random random = new Random();
        String base = value + "/s1A#24@!~*~(8*)/" + slat + random.nextInt();
        return DigestUtils.md5DigestAsHex(base.getBytes());
    }

    /**
     * 原生的加密方法
     *
     * @param value 需要加密的字符串
     * @return 加密后的字符串
     */
    public static String md5(String value) {
        try {
            value = value + slat;
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(value.getBytes("UTF8"));
            byte message[] = messageDigest.digest();
            String result = "";
            for (int i = 0; i < message.length; i++) {
                result += Integer.toHexString((0x000000FF & message[i]) | 0xFFFFFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

